package com.csd.action.message;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.csd.helper.IConstant;
import com.csd.pojo.Message;
import com.csd.service.IMessageService;
import com.csd.vo.UserVO;

/**
 * 
 * Action:获取信息：已读+未读
 * 
 * @author csd
 * @version 1.0
 * 
 * <br>
 *          修订时间：2009-12-17 <br>
 *          修改原因：创建文件 <br>
 *          修改人：何文深 <br>
 * 
 */
public class MessageListAction {
	
	private Integer numInPage;

	private Integer currPage;
	
	private Integer totalPage;

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalNum) {
		this.totalPage = totalNum;
	}


	private List<Message> newMessageList;
	
	private List<Message> oldMessageList;
	
	private IMessageService messageService;

	public Integer getNumInPage() {
		return numInPage;
	}

	public void setNumInPage(Integer numInPage) {
		this.numInPage = numInPage;
	}

	public Integer getCurrPage() {
		return currPage;
	}

	public void setCurrPage(Integer currPage) {
		this.currPage = currPage;
	}
	
	public List<Message> getNewMessageList() {
		return newMessageList;
	}

	public void setNewMessageList(List<Message> newMessageList) {
		this.newMessageList = newMessageList;
	}

	public List<Message> getOldMessageList() {
		return oldMessageList;
	}

	public void setOldMessageList(List<Message> oldMessageList) {
		this.oldMessageList = oldMessageList;
	}

	public IMessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}
	
	
	public String execute() throws Exception {
		String text = "fail";
		HttpSession httpSession = ServletActionContext.getRequest().getSession();
		UserVO userVO = (UserVO)httpSession.getAttribute(IConstant.userVO);
		if (userVO != null) {
			List<Message> old = messageService.listOldMessage();
			
			if( this.currPage == null  || this.currPage <= 0 )
				this.currPage = 1;
			
			if( this.numInPage == null || this.numInPage <= 0)
				this.numInPage = IConstant.numInPage;
			
			if(oldMessageList == null)
			{
				oldMessageList = new ArrayList<Message>();
			}
			
			if(old == null)
			{
				old = new ArrayList<Message>();
			}
			
			oldMessageList.clear();
			
			for(int i = (this.currPage-1)*this.numInPage; 
					i < this.currPage*this.numInPage && i < old.size(); 
					i++)
			{
				oldMessageList.add( old.get(i) );
			}
			

			this.totalPage = old.size() / this.numInPage;
			
			if(old.size() % this.numInPage != 0)
				this.totalPage++;
			
			newMessageList = messageService.listNewMessage();
			text = "success";
		}
		
		return text;
	}
}
